<template>
  <div>
    <common-head :title="$route.name" />
    <div class="container">
      <div class="items">
        <van-swipe-cell v-for="item in items" :key="item.id">
          <div class="item">
            <van-checkbox @change="itemStateChange(item.key,item.selected)" v-model="item.selected" />
            <van-card
              :num="item.number"
              :price="item.price"
              :title="item.name"
              :thumb="item.pic"
            >
              <template #footer>
                <van-stepper @change="itemNumChange(item.key,item.number)" v-model="item.number" />
              </template>
            </van-card>
          </div>
          <template #right>
            <van-button
             @click="delItem(item.key)"
              square
              text="删除"
              type="danger"
              class="delete-button"
            />
          </template>
        </van-swipe-cell>
      </div>
    </div>
    <!-- 提交订单栏 -->
    <van-submit-bar
      :price="total * 100"
      button-text="提交订单"
      @submit="onSubmit"
    >
      <van-checkbox v-model="allChecked">全选</van-checkbox>
    </van-submit-bar>
    <!-- /提交订单栏 -->
    <my-tbbar />
  </div>
</template>

<script>
import MyTbbar from '../../components/MyTabbar.vue'
import CommonHead from '../../components/CommonHead.vue'
import { fetchCartInfo, setItemNum, delItem, itemStateChange } from '_api'
import { getToken } from '_utils'
export default {
  name: 'Cart',
  data () {
    return {
      total: 0,
      items: [],
      number: 0
    }
  },
  created () {
    this.fetchCartInfo()
  },
  methods: {
    itemStateChange (key, selected) {
      // 修改商品的选中状态
      itemStateChange({
        key,
        selected,
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.items
          this.total = res.data.data.price
          this.number = res.data.data.number
        }
      })
    },
    delItem (key) {
      // 删除商品
      delItem({
        key,
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.items
          this.total = res.data.data.price
          this.number = res.data.data.number
        }
        if (res.data.code === 700) {
          // 购物车没有商品
          this.items = []
          this.total = 0
        }
      })
    },
    itemNumChange (key, number) {
      // 修改购物车购买数量
      setItemNum({
        key,
        number,
        token: getToken()
      }).then(res => {
        console.log(res)
        if (res.data.code === 0) {
          this.items = res.data.data.items
          this.total = res.data.data.price
          this.number = res.data.data.number
        }
      })
    },
    fetchCartInfo () {
      // 获取购物车数据
      fetchCartInfo({
        token: getToken()
      }).then((res) => {
        if (res.data.code === 0) {
          this.items = res.data.data.items
          this.total = res.data.data.price
          this.number = res.data.data.number
        }
      })
    },
    onSubmit () {
      // 点击到订单页
      this.$router.push('/order')
    }
  },
  watch: {
    items (newVal) {
      // 调用 mutation 将购物车数据存储到vuex中
      this.$store.commit('cart/set_cart_info', {
        items: this.items,
        number: this.number
      })
    }
  },
  computed: {
    allChecked: {
      get () {
        return this.items.every((item) => item.selected)
      },
      set (selected) {
        // 修改全选状态 setter 触发 修改所有商品的选中状态
        const keys = []
        this.items.forEach(item => {
          keys.push(item.key)
        })
        itemStateChange({
          key: keys.join(','),
          selected,
          token: getToken()
        }).then((res) => {
          if (res.data.code === 0) {
            this.items = res.data.data.items
            this.total = res.data.data.price
            this.number = res.data.data.number
          }
        })
      }
    }
  },
  components: {
    MyTbbar,
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.van-submit-bar {
  bottom: 50px;
}
.container {
  padding-top: 46px;
  padding-bottom: 100px;
  .item {
    display: flex;
    background: #fafafa;
    .van-checkbox {
      margin: 0 5px;
    }
    .van-card {
      flex: 1;
    }
  }
  .delete-button {
    height: 100%;
  }
}
</style>
